package com.sky.service.adminImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static com.sky.constant.StatusConstant.ENABLE;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Override
    @Transactional
    public void add(DishDTO dishDTO) {
        //将dishDTO数据拷贝到dish实体类中,调用
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.add(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!flavors.isEmpty()){
            flavors.forEach(fl ->{
                fl.setDishId(id);
            });
            dishFlavorMapper.add(flavors);
        }

    }

    @Override
    public PageResult<DishVO> page(DishPageQueryDTO pageQueryDTO) {
        PageResult<DishVO> result = new PageResult<>();
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        List<DishVO> DishList=dishMapper.selectPage(pageQueryDTO);
        Page<DishVO> page=(Page<DishVO>) DishList;
        result.setTotal(page.getTotal());
        result.setRecords(page.getResult());
        return result;
    }

    @Override
    public Dish getDishById(Long id) {
        return dishMapper.selectById(id);
    }

    @Override
    @Transactional
    public void deleteBatches(List<Long> ids) {
        //起售菜品不能删除
        for(long id:ids){
            Dish byId = getDishById(id);
            if (Objects.equals(byId.getStatus(), ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //菜品关联了套餐不能删除
        List<Long> mealByDishId = setMealDishMapper.getMealByDishId(ids);
        if (mealByDishId!=null&& !mealByDishId.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        dishMapper.deleteBatches(ids);
        dishFlavorMapper.deleteBatches(ids);
    }

    @Override
    public DishVO getDishByIdWithFlavors(Long id) {
        DishVO dishVO = new DishVO();
        Dish dish = dishMapper.selectById(id);
        List<DishFlavor> dishFlavor=dishFlavorMapper.selectByDishId(id);
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //判断有没有改
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null&&!flavors.isEmpty()){
            //删除之前的
            dishFlavorMapper.deleteByDishId(dish.getId());
            for (DishFlavor df:flavors){
                df.setDishId(dish.getId());
            }
            //插入新的
            dishFlavorMapper.add(flavors);
        }
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.update(dish);
    }

    @Override
    public List<Dish> getDishesByCategoryId(Long categoryId) {
        return dishMapper.selectByCategoryId(categoryId);
    }

    @Override
    public List<DishVO> getDishesWithFlavorsByCategoryId(Long categoryId) {
        Dish dish=Dish.builder()
                .categoryId(categoryId)
                .status(ENABLE)
                .build();
        List<DishVO> dishVOS = dishMapper.selectDishesByCategoryId(dish);
        dishVOS.forEach(dishes->{
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(dishes.getId());
            dishes.setFlavors(dishFlavors);
        });
        return dishVOS;
    }
}
